package com.u.finance.check.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.u.finance.check.entity.Loans;
import com.u.finance.check.entity.PayRecord;
import com.u.finance.check.mapper.LoansMapper;
import com.u.finance.check.service.ILoansService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.u.finance.check.service.IPayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alex Wu
 * @since 2025-03-31
 */
@Service
public class LoansServiceImpl extends ServiceImpl<LoansMapper, Loans> implements ILoansService {


    @Autowired
    private IPayRecordService payRecordService;

    @Scheduled(cron = "0/5 * * * * ?")
    public void checkLoans() {
        // 查找所有今天下放的数据，然后再payRecordService中查找是否有对应的支付记录
        // 获取昨天所有贷款记录
        List<Loans> loansList = this.list(new QueryWrapper<Loans>().lambda().ge(Loans::getPayoutTime, LocalDate.now().minusDays(1)).lt(Loans::getPayoutTime, LocalDate.now()));
        for (Loans loan : loansList) {
            boolean hasPayRecord = payRecordService.getOne(new QueryWrapper<PayRecord>().lambda().eq(PayRecord::getLoansId, loan.getId())) != null; // 检查是否有对应的支付记录
            if (!hasPayRecord) {
                // 处理没有支付记录的贷款
                System.out.println("Loan ID: " + loan.getId() + " has no payment record.");
                notifyPay(loan.getId());
            }
        }
    }

    private Boolean notifyPay(Long id) {
        // 发送短信通知
        return true;
    }
}
